// pages/editProduct/editProduct.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    classification: [],
    reports:[]
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  // 获取产品名称
  nameVal: function (e) {
    var productInfo = this.data.productInfo;
    productInfo.name = e.detail.value;
    this.setData({
      productInfo: productInfo
    })
  },
  // 获取产品介绍
  descVal: function (e) {
    var productInfo = this.data.productInfo;
    productInfo.desc = e.detail.value;
    this.setData({
      productInfo: productInfo
    })
  },
  // 上传图片
  upImgs: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '上传中。。。',
        })
        wx.uploadFile({
          url: app.globalData.root + 'api/file/uploadImage', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            token: app.globalData.token
          },
          success(res) {
            wx.hideLoading();
            const data = JSON.parse(res.data);
            console.log(data.data.fileName);
            var productInfo = _this.data.productInfo;
            productInfo.imageList[0] = data.data.fileName;
            _this.setData({
              productInfo: productInfo
            })
            //do something
          }
        })
      }
    })
  },
  // 上传视频
  upVideo: function () {
    var _this = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePath = res.tempFilePath;
        // console.log(res);
        wx.showLoading({
          title: '上传中。。。',
        })
        wx.uploadFile({
          url: app.globalData.root + 'api/file/uploadVideo', //仅为示例，非真实的接口地址
          filePath: tempFilePath,
          name: 'file',
          header: {
            token: app.globalData.token
          },
          success(res) {
            wx.hideLoading();
            const data = JSON.parse(res.data);
            console.log(data.data.fileName);
            var productInfo = _this.data.productInfo;
            productInfo.videoList[0] = data.data.fileName;
            _this.setData({
              productInfo:productInfo
            })
            //do something
          }
        })
      }
    })
  },
  // 上传pdf
  upReport: function (e) {
    var _this = this;
    var pdfIndex = e.currentTarget.dataset.id;
    wx.chooseMessageFile({
      count: 1, //能选择文件的数量
      type: 'file', //能选择文件的类型,我这里只允许上传文件.还有视频,图片,或者都可以
      success(res) {
        var size = res.tempFiles[0].size;
        var filename = res.tempFiles[0].name;
        var newfilename = filename + "";
        console.log(res);
        if (newfilename.indexOf(".pdf") == -1) { //我还限制了文件的大小和具体文件类型
          wx.showToast({
            title: '格式必须为pdf！',
            icon: "none",
            duration: 2000,
            mask: true
          })
        } else {
          var business_license = res.tempFiles[0].path;
          wx.showLoading({
            title: '上传中。。。',
          })
          wx.uploadFile({
            url: app.globalData.root + 'api/file/uploadPdf', //上传的路径
            filePath: business_license, //刚刚在data保存的文件路径
            header: {
              token: app.globalData.token
            },
            name: 'file', //后台获取的凭据
            success(res) {
              wx.hideLoading();
              console.log(res);
              var data = JSON.parse(res.data);
              var reports = _this.data.reports;
              if(pdfIndex=="-1") {
                reports.push(data.data.fileName);
              }else {
                reports[pdfIndex] = data.data.fileName;
              }
              console.log(reports);
              _this.setData({
                reports: reports
              })
            }
          })

        }
      }
    })
  },
    // 发布
    release: function () {
      var _this = this;
      var reportsStr = '';
      var reports = _this.data.reports;
      for (var i = 0; i < reports.length; i++) {
        if (i == reports.length - 1) {
          reportsStr += reports[i]
        } else {
          reportsStr += reports[i] + ','
        }
      }
      console.log(reportsStr);
      var classification = _this.data.classification;
      var index = _this.data.index;
      var productInfo = _this.data.productInfo;
      var nameVal = productInfo.name,
        img = productInfo.imageList[0],
        descVal = productInfo.desc,
        video = productInfo.videoList[0];
      if (nameVal == '' || img == '' || descVal == '' || reportsStr == '' || video == '') {
        wx.showToast({
          title: '请填写完整！',
          icon: "none"
        })
        return;
      }
      wx.showLoading({
        title: '发布中。。。',
      })
      wx.request({
        url: app.globalData.root + 'api/product/modifyProductInfo',
        data: {
          image: img,
          name: nameVal,
          category_id: classification[index].category_id,
          desc: descVal,
          certificate: reportsStr,
          video: video,
          id:_this.data.id
        },
        header: {
          token: app.globalData.token
        },
        method: 'POST',
        success: function (res) {
          wx.hideLoading();
          console.log(res);
          if (res.data.code == 1) {
            wx.showToast({
              title: '发布成功！',
              icon: "none"
            })
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
  
          } else {
            wx.showToast({
              title: res.data.errorMsg,
              icon: "none"
            })
          }
        },
      })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    var id = options.id;
    _this.setData({
      id: id
    })
    console.log(id);
    wx.showLoading({
      title: '加载中。。。',
    })
    wx.request({
      url: app.globalData.root + 'api/product/getCategoryList',
      data: {},
      header: {
        token: app.globalData.token
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          _this.setData({
            classification: res.data.data
          })
          wx.request({
            url: app.globalData.root + 'api/product/getMyProductInfo',
            data: {
              id: _this.data.id
            },
            header: {
              token: app.globalData.token
            },
            method: 'POST',
            success: function (res) {
              wx.hideLoading();
              console.log(res);
              if (res.data.code == 1) {
                var classification = _this.data.classification;
                var category_id = res.data.data.category_id;
                var index = 0;
                for (var i = 0; i < classification.length; i++) {
                  if (category_id == classification[i].category_id) {
                    index = i;
                  }
                }
                var reports = res.data.data.certificateList;
                _this.setData({
                  productInfo: res.data.data,
                  index: index,
                  reports:reports
                })

              } else {
                wx.showToast({
                  title: res.data.errorMsg,
                  icon: "none"
                })
              }
            },
          })
        } else {
          wx.showToast({
            title: res.data.errorMsg,
            icon: "none"
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})